package com.br.android.widingwheels.db;

import com.br.android.widingwheels.R;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class WidingWheelsDataBase extends SQLiteOpenHelper{

	private static final String DATABASE_NAME = "widingwheels.db";
	
	private static final int DATABASE_VERSION = 1;
	
	private Context context;
	
	public WidingWheelsDataBase(Context context){
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
		this.context = context;
	}
	
	@Override
	public void onCreate(SQLiteDatabase database) {
		String[] sql = context.getString(R.string.table_create).split("\n");
		for(int i = 0; i < sql.length; i++){
			Log.i("SQL: ", sql[i]);
		}
		try {
			execMultipleSQL(database, sql);
			database.setTransactionSuccessful();
		} catch (Exception e) {
			Log.e("ERRO AO CRIAR TABELA", e.getMessage());
		} finally {
			database.endTransaction();
		}
	}	

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	
	private void execMultipleSQL(SQLiteDatabase db, String[] sql){
		for(String s : sql){
			if (s.trim().length() > 0){
				db.execSQL(s);
			}
		}
	}
	

}
